<%--
  Created by IntelliJ IDEA.
  User: diaominyh
  Date: 2022/3/31
  Time: 22:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增工作笔记</title>
    <script src="jquery/jquery-1.8.3.min.js"></script>
</head>
<body>
<div>
    <div align="center">
        <h2>我的工作笔记</h2>
        <form method="post" action="#">
            <input type="hidden" name="opr" value="add">
            <table>
                <tr>
                    <td>标题：</td>
                    <td><input type="text" name="title" id="title"><span id="hint"></span></td>
                </tr>
                <tr>
                    <td>内容：</td>
                    <td><textarea name="content" id="cont" style="height: 70px;width: 300px"></textarea></td>
                </tr>
                <tr>
                    <td>类别：</td>
                    <td>
                        <select name="type" id="type">
                            <option value="0">-请选择标题-</option>
                            <option value="1">技术</option>
                            <option value="2">行政</option>
                            <option value="3">人事</option>
                        </select>
                    </td>
                </tr>
            </table>
            <input type="button" value="提交" id="btn">
        </form>
    </div>
</div>
<script type="text/javascript">
    $("#title").blur(function(){
        $.ajax({
            "url": "working",
            "type": "post",
            "data": "opr=getCount&title=" + $("#title").val(),
            "dateType": "text",
            "success": function (data) {
                if (data === "true") {
                    $("#hint").html("标题已经存在，请重新输入！");
                } else {
                    if($("#title").val()!==""){
                        $("#hint").html("标题可用！");
                    }
                }
            }
        });
    });
    $("#btn").click(function () {
        let bool = true;
        if ($("#cont").val() === "") {
            bool = false;
            alert("标题不能为空，请填写！");
        } else if ($("#type").val() === "0") {
            bool = false
            alert("请选择类别！");
        }
        if (bool) {
            $.ajax({
                "url": "working",
                "type": "post",
                "data": "opr=add&title="+$("#title").val()+"&content="+$("#cont").val()+"&type="+$("#type").val(),
                "dateType": "text",
                "success": function (data) {
                    if (data === "true") {
                        alert("添加成功！");
                        location="${pageContext.request.contextPath}/working"
                    } else {
                        alert("添加失败！");
                    }
                }
            });
        }
    });
</script>
</body>
</html>
